<?
    class Principio_Model_TypeInCompany extends Principio_Model_Templatable implements PrincipioLab_IDAble
    {
        use PrincipioLab_TCollection;

        private $model_company_abstract;
        private $model_type;


        public function __construct(Principio_Model_Company_Abstract $model_company_abstract, Principio_Model_Type $model_type)
        {
            parent::__construct();

            $this->model_company_abstract = $model_company_abstract;
            $this->model_type = $model_type;

            $this->initTemplates();
        }


        public function _getID() { return $this; }


        public static function makeCollection(array $values = null, PrincipioLab_IComparator $comparator = null)
        {
            $collection = static::getLinkedList(is_null($comparator) ? new Principio_Comparator_TypeInCompany() : $comparator);

            if ((!is_null($values)) && (!is_null($values['values'])))
            {
                $model_company_abstract = (isset($values['model_company_abstract']) ? $values['model_company_abstract'] : null);
                $model_type = (isset($values['model_type']) ? $values['model_type'] : null);

                foreach ($values['values'] as $value)
                {
                    $collection->addItem(new Principio_Model_TypeInCompany(
                        is_null($model_company_abstract) ? Principio_Model_Company_AbstractMapper::fetch($value['id']) : $model_company_abstract,
                        is_null($model_type) ? Principio_Model_TypeMapper::fetch($value['id']) : $model_type));
                }
            };

            return $collection;
        }


        public static function fromArray(array $values)
        {
            return new Principio_Model_TypeInCompany($values['model_company'], Principio_Model_TypeMapper::fetch($values['values']['id']));
        }


        public function getModelType() { return $this->model_type; }
        public function getModelCompany() { return $this->model_company_abstract; }
        public function getSpaCollection() { return Principio_Model_SpaMapper::getByTypeInCompany($this); }


        public function add() { return $this->addTemplates(); }
        public function save() { return $this->saveTemplates(); }


        public function delete($delete_with_spa = true)
        {
            if ($delete_with_spa)
            {
                foreach ($this->getSpaCollection() as $model_spa)
                    $model_spa->delete(false, false);
            };

            $this->deleteTemplates();
        }


        protected function fetchTemplates() { return Principio_Model_TypeInCompanyExtTemplateMapper::fetch($this); }
        public function addTemplates() { return Principio_Model_TypeInCompanyExtTemplateMapper::insert($this); }
        public function saveTemplates() { return Principio_Model_TypeInCompanyExtTemplateMapper::update($this); }
        public function deleteTemplates() { return Principio_Model_TypeInCompanyExtTemplateMapper::delete($this); }
    }
?>